package com.xiao.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.common.dto.SysMenuDto;
import com.xiao.common.lang.Result;
import com.xiao.entity.SysMenu;
import com.xiao.entity.SysRoleMenu;
import com.xiao.entity.SysUser;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者:小东
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {


/**
 * @author: xiao
 * @params: 获取当前用户的菜单和权限（导航栏）
 * @dateTime: 2022/6/25
 */
    @GetMapping("/nav")
    public Result nav(Principal principal){
        SysUser sysUser = sysUserService.getByUsername(principal.getName());

        //获取权限信息
        String authorityInfo = sysUserService.getUserAuthorityInfo(sysUser.getId());
        String[] authorityInfoArray = StringUtils.tokenizeToStringArray(authorityInfo, ",");

        //获取对应用户的导航栏信息
      List<SysMenuDto> navs = sysMenuService.getCurrentUserNav();

        return Result.succ(MapUtil.builder()
                .put("authoritys",authorityInfoArray)
                .put("nav",navs)
                .map()
        );
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name="id") Long id){
        return Result.succ(sysMenuService.getById(id));
    }

    //菜单管理功能的增删改查
/**
 * @author: xiao
 * @params: 菜单列表信息
 * @dateTime: 2022/6/25
 */
    @GetMapping("/list")
    public Result list(){
        List<SysMenu> menus = sysMenuService.tree();
        return Result.succ(menus);
    }

/**
 * @author: xiao
 * @params: 菜单新增
 * @dateTime: 2022/6/25
 */
    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysMenu sysMenu){
        sysMenu.setCreated(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return Result.succ(sysMenu);
    }
/**
 * @author: xiao
 * @params: 菜单修改
 * @dateTime: 2022/6/25
 */
    @PostMapping("/update")
    public Result update(@Validated @RequestBody SysMenu sysMenu){
        sysMenu.setUpdated(LocalDateTime.now());
        sysMenuService.updateById(sysMenu);
        //清除所有与改菜单相关的缓存
        sysUserService.clearUserAuthorityInfoByMenuId(sysMenu.getId());
        return Result.succ(sysMenu);
    }

/**
 * @author: xiao
 * @params: 菜单删除
 * @dateTime: 2022/6/25
 */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        //判断是否有子菜单
        int count = sysMenuService.count(new QueryWrapper<SysMenu>().eq("parent_id", id));
        if (count >0){
            return Result.fail("请先删除子菜单");
        }
        //清除所有与改菜单相关的缓存
        sysUserService.clearUserAuthorityInfoByMenuId(id);
        sysMenuService.removeById(id);
        //删除中间关联表的记录
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("menu_id",id));

        return Result.succ("");
    }

}
